<?php
/***************************************************************************
 * 
 * Copyright (c) 2010 , Inc. All Rights Reserved
 * $Id$:FcryptTest.php,2010/05/20 13:53:57 
 * 
 **************************************************************************/
 
 
 
/**
 * @file FcryptTest.php
 * @author huqingping(huqingping@)
 * @date 2010/05/20 13:53:57
 * @version 1.0 
 * @brief 
 *  
 **/

require_once dirname(__FILE__).'/../Fcrypt.php';

class FcryptTest extends PHPUnit_Framework_TestCase
{
	var $f;
	function __construct()
	{
		$this->f = MCUtil::load('Fcrypt');
	}

	function testActual()
	{
		$json = '{"__need_login__":false,"_pn":0,"_tn":1,"_rn":10,"sh_i":"1","album_i":"4","biz_name":"","album_name":"tianyuanfengguang","album_num":1,"pic_list":[{"pic_id":"412652731185721001","pic_name":"pic1","pic_desc":"pic1","is_front_page":"0","is_feature_page":"0","user_name":"wuhui","user_id":"1","pic_create_time":"11111"}],"album_list":[{"album_id":"4","album_name":"tianyuanfengguang","cov_id":"0","album_create_time":"1","num":1}],"_arr_get_raw":[],"_url":"\/biz\/1\/pic\/4","_uri":"\/biz\/1\/pic\/4?_d=1","_host":"db-omega.db01.:8060","_now":1279612130,"__user__":{"uid":0,"uname":""}}';
		$arr = json_decode($json,true);
		$this->f->encodeArray2($arr);

		$this->assertFalse('4'==$arr['album_list'][0]['album_id']);
	}

	function testIsId()
	{
		$this->assertFalse($this->f->isId('asdas'));
		$this->assertFalse($this->f->isId('12312'));
		$this->assertFalse($this->f->isId('1232sdf'));
		$this->assertTrue($this->f->isId('s12837dkd2kd82ks81k2387e'));
		$this->assertFalse($this->f->isId('s12837dkd2kd82ks81k2387!'));
	}

	function testStringSign()
	{
		$this->f->stringSign('xxx');
	}

	function testEncodeDecode()
	{
		$this->assertEquals(24, strlen($this->f->encode(232323)));
		$this->assertEquals(24, strlen($this->f->encode(0)));
		$this->assertEquals(0, $this->f->decode($this->f->encode(0)));
		$this->assertEquals(-1, $this->f->decode($this->f->encode(-1)));
		$this->assertEquals(1, $this->f->decode($this->f->encode(1)));

		$max = 0xFFFFFFFF;
		$num1 = ($max << 32) | $max;
		$num2 = (0x7FFFFFFF << 32) | $max;
		$this->assertEquals($num1, $this->f->decode($this->f->encode($num1)));
		$this->assertEquals($num2, $this->f->decode($this->f->encode($num2)));
	}

	function testEncodeDecodeString()
	{
		$tmp1 = 'as';
		$tmp2 = '1,0,a';
		$tmp3 = 'as,,1,2,34';	
		$this->assertFalse(
			$this->f->encodeString($tmp1) ||
			$this->f->encodeString($tmp2) ||
      	   $this->f->encodeString($tmp3) 	
		);
		$this->assertFalse(
			$this->f->decodeString($tmp1) ||
			$this->f->decodeString($tmp2) ||
      	   $this->f->decodeString($tmp3) 	
		);
		$max = 0xFFFFFFFF;
		$num1 = ($max << 32) | $max;
		$num2 = (0x7FFFFFFF << 32) | $max;
		$str = $str1 = '1,0,-1,22,,'.$num1.','.$num2; 
		$this->assertTrue($this->f->encodeString($str1));
		$this->assertEquals(83, strlen($str1));	
		$this->assertTrue($this->f->decodeString($str1));
		$this->assertEquals($str1, $str1);

		$num = 1;
		$this->assertTrue($this->f->encodeString($num));
		$this->assertTrue($this->f->decodeString($num));
		$this->assertEquals(1,$num);
	}

	function testEncodeDecodeArray()
	{
		$arr = array(12,23,'xxd'=>2,'xc'=>'sdf',0,array(12,3),'cc'=>array('ok'));
		$this->assertTrue($this->f->encodeArray($arr));

		$arr0 = array(12,23,'xxid'=>'as','xc'=>'sdf',0,array(12,3),'cc'=>array('ok'));
		$this->assertFalse($this->f->encodeArray($arr0));

		$arr2 = $arr1 = array('xid'=>1,'x'=>1,'nid'=>'0','id'=>'2','aid'=>array('cid'=>1,2,'vid'=>0));
		$this->assertTrue($this->f->encodeArray($arr1));
		$this->assertEquals('0', $arr1['nid']);
		$this->assertEquals(24, strlen($arr1['xid']));
		$this->assertEquals(24, strlen($arr1['id']));
		$this->assertEquals(24, strlen($arr1['aid']['cid']));
		$this->assertEquals(1, strlen($arr1['aid']['vid']));

		$this->assertTrue($this->f->decodeArray($arr1));
		$this->assertEquals($arr2['nid'], $arr1['nid']);
		$this->assertEquals($arr2['xid'], $arr1['xid']);
		$this->assertEquals($arr2['id'], $arr1['id']);
		$this->assertEquals($arr2['aid']['cid'], $arr1['aid']['cid']);
		$this->assertEquals($arr2['aid']['vid'], $arr1['aid']['vid']);
	}

	function testEncodeDecodeArray2()
	{
		$arr = array(12,23,'xxd'=>2,'xc'=>'sdf',0,array(12,3),'cc'=>array('ok'));
		$this->assertTrue($this->f->encodeArray2($arr));

		$arr0 = array(12,23,'xxid'=>'as','xc'=>'sdf',0,array(12,3),'cc'=>array('ok'));
		$this->assertFalse($this->f->encodeArray2($arr0));

		$arr2 = $arr1 = array('xid'=>1,'x'=>1,'id'=>'2','aid'=>array('cid'=>1,2,'vid'=>0));
		$this->assertTrue($this->f->encodeArray2($arr1));
		$this->assertEquals(24, strlen($arr1['xid']));
		$this->assertEquals(24, strlen($arr1['id']));
		$this->assertEquals(24, strlen($arr1['aid']['cid']));
		$this->assertEquals(1, strlen($arr1['aid']['vid']));

		$this->assertTrue($this->f->decodeArray($arr1));
		$this->assertEquals($arr2['xid'], $arr1['xid']);
		$this->assertEquals($arr2['id'], $arr1['id']);
		$this->assertEquals($arr2['aid']['cid'], $arr1['aid']['cid']);
		$this->assertEquals($arr2['aid']['vid'], $arr1['aid']['vid']);
	}

	function testEncodeDecodeObject()
	{
		$obj = array('a'=>1);
		$enc = $this->f->encodeObject($obj);
		$dec = $this->f->decodeObject($enc);
		$this->assertTrue(is_array($dec));
		$this->assertEquals($obj['a'], $dec['a']);

		$obj = new stdClass();
		$obj->b = array('c'=>'1');
		$enc = $this->f->encodeObject($obj);
		$dec = $this->f->decodeObject($enc);
		$this->assertTrue(is_object($dec));
		$this->assertEquals($obj->b['c'], $dec->b['c']);
	}

}






/* vim: set ts=4 sw=4 sts=4 tw=100 noet: */
?>
